<template>
  <div class="app-container">
    <el-dialog
      title="专属岗位新增"
      :visible.sync="dialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="handleClose"
    >
      <el-form
        ref="addRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item
          label="岗位名称:"
          prop="JobName"
        >
          <el-input v-model="ruleForm.JobName" />
        </el-form-item>
        <el-form-item
          label="岗位编制:"
          prop="jobExpNum"
        >
          <el-input
            v-model="ruleForm.jobExpNum"
            type="number"
          />
        </el-form-item>
        <el-form-item
          label="岗位职责:"
          prop="JobDuty"
        >
          <el-input
            v-model="ruleForm.JobDuty"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="handleClose">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="submitForm()"
        >
          保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { GetData, BatchTableOperateRequestByCRUD } from '@/api/Hr';
export default {
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        },
        departmentData: {
            type: Object,
            default: () => ({})
        },
    },
    data() {
        return {
            rules: {
                JobName: [
                    { required: true, message: '请输入岗位名称', trigger: 'blur' },
                ],
                jobExpNum: [
                    { required: true, message: '请输入岗位编制', trigger: 'blur' }
                ]
               
            },
            ruleForm: {}
        }
    },
    watch: {
        dialogVisible(val) {
            if (val) {
                this.init()
            }
        }
    },
    methods: {
        init() {
            this.ruleForm = {}
            this.$set(this.ruleForm, 'jobExpNum', 0)
        },
        resetForm() {
            this.$refs.addRef.resetFields()
        },
        submitForm() {
            this.$refs.addRef.validate((valid) => {
                if (!valid) {
                    console.log('表单验证失败');
                    return;
                }

                const data = {
                    "Table": [
                        {
                            "Name": "Base_JobInfo",
                            "Type": "数据库表",
                            "DbName": "task_demo",
                            "DISTINCT": true,
                            "Filter": {
                                "Type": "and",
                                "Filters": [
                                    {
                                        "Type": "and",
                                        "Filters": [
                                            {
                                                "Type": "cond",
                                                "Field": "JobName",
                                                "Operator": "equal",
                                                "Value": null,
                                                "ValueFun": {
                                                    "Type": "GetConstValue",
                                                    "Value": this.ruleForm.JobName
                                                }
                                            },
                                            {
                                                "Type": "cond",
                                                "Field": "DepLevelCode",
                                                "Operator": "equal",
                                                "Value": null,
                                                "ValueFun": {
                                                    "Type": "GetConstValue",
                                                    "Value": this.departmentData.DepLevelCode
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        }
                    ]
                };

                GetData(data, 'DA242D6EF89B69A7ED5D2728408833AB')
                    .then(res => {
                        if (res.Code === 200) {
                            if (res.Result.data.Items.length === 0) {
                                const adddata = [
                                    {
                                        TableName: 'task_demo@Base_JobInfo',
                                        CrudModel: {
                                            Added: [{
                                                rowid:Math.random().toString(36).substring(2) + Date.now().toString(36),
                                                JobName: this.ruleForm.JobName,
                                                jobExpNum: this.ruleForm.jobExpNum,
                                                JobDuty: this.ruleForm.JobDuty,
                                                JobType: 1,
                                                DepLevelCode: this.departmentData.DepLevelCode,
                                                DepLevel: this.departmentData.DepLevel,
                                                DepName: this.departmentData.DepName,
                                                Depid: this.departmentData.DepID,
                                                JobLevel:0
                                            }],
                                            Changed: [],
                                            Deleted: []
                                        }
                                    }
                                ]
                                BatchTableOperateRequestByCRUD(adddata, 'DA242D6EF89B69A7ED5D2728408833AB')
                                    .then(res => {
                                        if (res.Code === 200) {
                                            this.$message.success('保存成功');
                                            this.$emit('jobrefresh');
                                        }
                                        else {
                                            this.$message.error('保存失败');
                                        }
                                    })
                                    .catch(err => {
                                        console.error(err);
                                        this.$message.error('保存失败');
                                    })
                            }
                            else {
                                this.$message.error('"岗位已存在！！！');
                                this.$set(this.ruleForm, 'JobName', '')
                            }
                        }
                        else {
                            this.$message.error('保存失败');
                        }
                    })
                    .catch(err => {
                        console.error(err);
                        this.$message.error('保存失败');
                    });
            });
        },
        handleClose() {

            this.$emit('close');
            this.resetForm();
        }
    }
};
</script>

<style scoped>
::v-deep .el-dialog__header {
    text-align: left;
}

.app-container {
    padding: 20px;
}

.dialog-footer {
    text-align: right;
    margin-top: 20px;
}
</style>